package club.haomoon.boss.service.impl;

import club.haomoon.boss.service.IDecorateTabbarService;
import club.haomoon.boss.validate.article.DecorateTabsValidate;
import club.haomoon.boss.vo.article.DecorateTabbarVo;
import club.haomoon.boss.vo.article.DecorateTabsListsVo;
import club.haomoon.boss.vo.article.DecorateTabsStyleVo;
import club.haomoon.core.entity.article.DecorateTabbarData;
import club.haomoon.core.mapper.article.DecorateTabbarDataMapper;
import club.haomoon.core.util.ConfigUtil;
import club.haomoon.core.util.DateUtil;
import club.haomoon.core.util.MapUtil;
import club.haomoon.core.util.StringUtils;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * -------------------------
 * @Description: 底部导航服务实现类
 * @Author: Wangjianwei
 * @Date: 2025/02/18 17:36
 * -------------------------
 */
@Service
public class DecorateTabbarServiceImpl implements IDecorateTabbarService {

    @Resource
    DecorateTabbarDataMapper decorateTabbarDataMapper;

    /**
     * 底部导航详情
     * @return DecorateTabbarVo
     */
    @Override
    public DecorateTabbarVo detail() {

        List<DecorateTabbarData> list = decorateTabbarDataMapper.selectList(
                new QueryWrapper<DecorateTabbarData>()
                    .orderByAsc("id"));

        List<DecorateTabsListsVo> tabList = new LinkedList<>();
        for (DecorateTabbarData tab: list) {
            DecorateTabsListsVo vo = new DecorateTabsListsVo();
            vo.setId(tab.getId());
            vo.setName(tab.getName());
            vo.setSelected(tab.getSelected());
            vo.setUnselected(tab.getUnselected());
            vo.setLink(JSON.parse(tab.getLink()));
            vo.setIsShow(tab.getIsShow());
            vo.setCreateTime(DateUtil.timestampToDate(tab.getCreateTime()));
            vo.setUpdateTime(DateUtil.timestampToDate(tab.getUpdateTime()));
            tabList.add(vo);
        }

        String tabbar = ConfigUtil.get("tabbar", "style", "{}");

        DecorateTabbarVo response = new DecorateTabbarVo();
        DecorateTabsStyleVo styleVo = new DecorateTabsStyleVo();
        Map<String,String> styleMap = MapUtil.jsonToMap(tabbar);
        styleVo.setDefaultColor(styleMap.get("defaultColor"));
        styleVo.setSelectedColor(styleMap.get("selectedColor"));
        response.setStyle(styleVo);
        response.setList(tabList);
        return response;
    }

    /**
     * 底部导航保存
     * @param tabsValidate 参数
     */
    @Override
    @Transactional
    public void save(DecorateTabsValidate tabsValidate) {
        decorateTabbarDataMapper.delete(new QueryWrapper<DecorateTabbarData>().gt("id", 0));

        for (DecorateTabsListsVo obj : tabsValidate.getList()) {
            DecorateTabbarData model = new DecorateTabbarData();
            model.setName(obj.getName());
            model.setLink(JSON.toJSONString(obj.getLink()));
            model.setSelected(obj.getSelected());
            model.setUnselected(obj.getUnselected());
            model.setCreateTime(System.currentTimeMillis() / 1000);
            model.setUpdateTime(System.currentTimeMillis() / 1000);
            decorateTabbarDataMapper.insert(model);
        }

        if (StringUtils.isNotNull(tabsValidate.getStyle())) {
            ConfigUtil.set("tabbar", "style", JSON.toJSONString(tabsValidate.getStyle()));
        }
    }

}
